﻿using Customer.Read.API.Dtos;
using Customer.Domain;
using Customer.Read.API.Applcation.Command.ProcurementCommand;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using Customer.Read.API.Server;
using System.Diagnostics.Contracts;
using Customer.Domain.Dto;
using Microsoft.AspNetCore.Authorization;
using System.Security.Claims;
using System.IdentityModel.Tokens.Jwt;
using Customer.Read.API.Applcation.Command.RefundCommand;

namespace Customer.Read.API.Controllers
{
    /// <summary>
    /// 采购单管理（读）
    /// </summary>
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]
    
    public class ProcurementController : ControllerBase
    {
        IMediator mediator;
        httpshow _http;

        public ProcurementController(IMediator mediator, httpshow _http)
        {
            this.mediator = mediator;
            this._http = _http;
        }

        /// <summary>
        /// 反填采购单以及产品
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<ProcureProductDTO>> FanProduct([FromQuery] ProductFanCommand command)
        {
            ApiResult<ProcureProductDTO> result = new ApiResult<ProcureProductDTO>();
            try
            {
                result.Data = await mediator.Send(command);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }


        /// <summary>
        /// 显示采购单
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<Page<ProcureProductDTO>>> GetRrocurement([FromQuery]ProcurementSelCommand command)
        {
            ApiResult<Page<ProcureProductDTO>> result = new ApiResult<Page<ProcureProductDTO>>();
            try
            {
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                command.Office_Id = sid;
                result.Data = await mediator.Send(command);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 显示产品
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetProductList(string? productsCode, string? productsName, int pindex, int psize)
        {
            return Ok(await _http.GetProductList(productsCode, productsName,pindex, psize));
        }


        /// <summary>
        /// 显示库位
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetStorehouseList(string? StoreCode, string? StoreName, int pageIndex, int PageSize, int? wid)
        {
            return Ok(await _http.GetStorehouseList(StoreCode, StoreName, pageIndex, PageSize, wid));
        }


        /// <summary>
        /// 查看详情时的采购单
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<ProcureProductDTO>>> GetRrocurementDetail([FromQuery]ProcurementCheckCommmand command)
        {
            ApiResult<List<ProcureProductDTO>> result = new ApiResult<List<ProcureProductDTO>>();
            try
            {
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                command.Office_Id = sid;
                result.Data = await mediator.Send(command);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }



        /// <summary>
        /// 显示操作列的退货
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<GetRefundDto>>> GetRefundDetail([FromQuery] RefundListCommand command)
        {
            ApiResult<List<GetRefundDto>> result = new ApiResult<List<GetRefundDto>>();
            try
            {
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                command.Office_Id = sid;
                result.Data = await mediator.Send(command);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }



    }
}
